package com.fruits.login.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fruits.login.dao.ConfigResourceDAO;
import com.fruits.login.dao.ConfigVersionDAO;
import com.fruits.login.model.ConfigResourceInfo;
import com.fruits.login.model.ReturnMsg;
import com.fruits.login.model.ConfigVersionInfo;
import com.fruits.login.service.CheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author cyc
 */
@Service
public class CheckServiceImpl implements CheckService {

    @Autowired
    private ConfigResourceDAO resourceDAO;

    @Autowired
    private ConfigVersionDAO versionDAO;

    @Override
    public ReturnMsg checkVersion(ConfigVersionInfo versionInfo) {
        ReturnMsg msg = new ReturnMsg();
        try {
            ConfigVersionInfo versionInfo1 = versionDAO.getVersionInfo(versionInfo.getVersion(), versionInfo.getSysId());
            if (versionInfo1 != null) {
                if (versionInfo1.getDownUrl() == null || "".equals(versionInfo1.getDownUrl())) {
                    msg.setState(ReturnMsg.FAILE);
                } else {
                    msg.setState(ReturnMsg.SUCCESS);
                    JSONObject json = new JSONObject();
                    json.put("downUrl", versionInfo1.getDownUrl());
                    json.put("aduite", versionInfo1.getAduite());
                    msg.addJSONObject(json);
                }
            } else {
                msg.setState(3);
                msg.setInfo("未发现版本信息");
            }
        } catch (Exception e) {
            msg.setState(3);
            msg.setInfo("版本检测错误");
        }
        return msg;
    }

    @Override
    public ReturnMsg checkResource(ConfigResourceInfo configResourceInfo) {
        ReturnMsg msg = new ReturnMsg();
        try {
            ConfigResourceInfo returnMsg = resourceDAO.getResourceInfo(configResourceInfo);
            if (returnMsg != null) {
                msg.setState(ReturnMsg.SUCCESS);
                JSONObject json = new JSONObject();
                json.put("downUrl", returnMsg.getDownUrl());
                json.put("md5Code", returnMsg.getCode());
                msg.addJSONObject(json);
            } else {
                msg.setState(ReturnMsg.ERROR);
                msg.setInfo("资源不存在");
            }
        } catch (Exception e) {
            msg.setState(ReturnMsg.ERROR);
            msg.setInfo("资源检测错误");
        }
        return msg;
    }
}
